package com.atguigu.exer;

public class SaleTicket2 implements Runnable{
    Object object=new Object();
    private int ticket=1000;

    public static void main(String[] args) {
        SaleTicket2 saleTicket2=new SaleTicket2();
        Thread thread1=new Thread(saleTicket2);
        Thread thread2=new Thread(saleTicket2);
        Thread thread3=new Thread(saleTicket2);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }

    public void run(){
            while (true) {
                synchronized (object) {
                    if (ticket > 0) {
                        System.out.println(Thread.currentThread().getName() + "售票，票号为:" + ticket);
                        ticket--;
                    } else {
                        break;
                    }
                }
        }
    }
}
